﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Eneta.Portal.Common.Dto;
using Eneta.Portal.Common.Helpers;
using Eneta.Portal.Common.Repository;
using Eneta.Portal.Controls;
using Eneta.Portal.Literature.Controls;
using Microsoft.SharePoint;

namespace Eneta.Portal.Literature.Pages
{
    public class MagazineView : Page
    {
        protected Label LiteratureTitle;
        protected Label LiteraturePublisher;
        protected Label LiteraturePublishingYear;
        protected Literal PageTitle;
        protected LiteratureCommentAddControl LiteratureAddComment;

        protected HtmlImage LiteraturePicture;
        protected HtmlAnchor additionalInfoURL;
        protected HtmlAnchor AuthorProfileLink;
        protected HtmlImage AuthorImage;
        protected HtmlGenericControl AuthorName;
        protected HtmlGenericControl EntryDate;
        protected HtmlGenericControl EntryDescription;
        protected HtmlGenericControl LiteraturePageTitle;
        protected HtmlGenericControl LiteratureDetailView;
        protected TagsViewControl LiteratureTags;

        private int ItemId
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                {
                    int id = -1;
                    if (int.TryParse(Request.QueryString["Id"], out id))
                    {
                        return id;
                    }
                }
                return -1;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (ItemId == -1)
                return;
            var repository = new LiteratureRepository(SPContext.Current.Web);
            var item = repository.GetLiteratureItemById(ItemId);
            if (item.Status == SPModerationStatusType.Approved)
            {
                BindMagazineProperties(item);
            }
            else
            {
                LiteratureAddComment.Visible = false;
                if (SPContext.Current.Web.CurrentUser == null)
                {
                    LiteratureDetailView.Visible = false;
                } else
                {
                    if (!SPContext.Current.Web.CurrentUser.IsSiteAdmin)
                        LiteratureDetailView.Visible = false;
                    else
                        BindMagazineProperties(item);
                }                
            }

        }

        private void BindMagazineProperties(LiteratureDto item)
        {
            PageTitle.Text = item.Title;
            LiteratureTitle.Text = item.Title;
            LiteraturePicture.Alt = item.Title;
            LiteraturePicture.Src = item.LiteratureImageUrl;
            LiteraturePageTitle.InnerText = item.Title;
            LiteraturePublisher.Text = item.BookAuthors;
            if (!string.IsNullOrEmpty(item.PublishingYear))
            {
                LiteraturePublishingYear.Text = item.PublishingYear.Replace("|", " korda " );
            }
            AuthorProfileLink.HRef = UrlHelper.GetProfileUrl(item.PosterProfileId);
            AuthorName.InnerText = item.Author;
            AuthorImage.Src = UrlHelper.GetUserImageHandlerUrl(16, item.PosterProfileId);
            AuthorImage.Alt = item.Title;
            EntryDate.InnerText = item.PostedDate.ToString("dd.MM.yyyy");
            LiteraturePublisher.Text = item.BookAuthors;

            EntryDescription.InnerHtml = item.Summary;

            if (item.Keywords.Count != 0)
            {
                LiteratureTags.Keywords = item.Keywords;
            }

            if (!string.IsNullOrEmpty(item.AdditionalInfoURL))
            {
                additionalInfoURL.HRef = item.AdditionalInfoURL;
            }
        }
    }
}
